<template>
    <div class="post-list-container">
      <!-- 搜索框 -->
      <div class="search-box">
        <el-input
          v-model="searchKey"
          placeholder="搜索帖子标题或内容"
          clearable
          @change="handleSearch"
          class="search-input"
        >
          <template #prefix>
            <el-icon><search /></el-icon>
          </template>
        </el-input>
      </div>

        <!-- 新增分类筛选栏 -->
      <div class="category-scroll">
        <el-scrollbar always>
          <div class="category-list">
            <div
            :class="['category-item', { 'active': selectedCategory ==0 }]"
              @click="selectCategory(0)
              "
            >
              全部
            </div>
            <div
              v-for="category in categories"
              :key="category.categoryId"
              :class="['category-item', { 'active': selectedCategory === category.categoryId }]"
              @click="selectCategory(category.categoryId)"
            >
              {{ category.categoryName }}
            </div>
          </div>
        </el-scrollbar>
      </div>


      <!-- 帖子列表 -->
      <div
        class="post-list"
        v-infinite-scroll="loadMore"
        :infinite-scroll-disabled="loading"
        :infinite-scroll-distance="100"
      >
        <div
          v-for="post in postList"
          :key="post.id"
          class="post-item"
        >

            <div class="post-header">
                <el-avatar :src="post.avatar" size="small" />
                    <span class="username">{{ post.username }}</span>
                    <span class="post-time">{{ post.createTime}}</span>
            </div>
            <h3 class="post-title">{{ post.title }}</h3>
            <div class="post-content" :class="{ 'truncated': !post.expand }">
            <div v-html="post.content"></div>
            </div>
            <div class="post-footer">
            <!-- 标签区域 -->
            <div class="tags-section">
                <el-tag
                v-for="tag in post.tags"
                :key="tag"
                size="small"
                class="post-tag"
                >
                {{ tag }}
                </el-tag>
            </div>

            <!-- 统计信息 + 操作按钮 -->
            <div class="meta-section">
                <div class="stats">
                <span class="stat-item">
                    <el-icon><View /></el-icon>
                    {{ post.viewCount || 0 }}
                </span>
                <span class="stat-item">
                    <el-icon><Comment /></el-icon>
                    {{ post.commentCount || 0 }}
                </span>
                </div>
        <el-button
          type="primary"
          size="small"
          @click="viewDetail(post.postId)"
          class="detail-btn"
        >
          查看详情
        </el-button>
      </div>
    </div>

        </div>

        <!-- 加载状态 -->
        <div class="loading-tip">
          <el-icon v-if="loading" class="is-loading">
            <loading />
          </el-icon>
          <span v-else>没有更多数据了</span>
        </div>
      </div>

      <!-- 返回顶部按钮 -->
      <el-backtop
        target=".post-list-container"
        :visibility-height="200"
      >
        <el-icon :size="24">
          <caret-top />
        </el-icon>
      </el-backtop>
    </div>
  </template>

  <script setup>
  import { Search,
           CaretTop,
           Loading,
           Comment,
           View
   } from '@element-plus/icons-vue'
  import { ref,onMounted, reactive, getCurrentInstance, nextTick } from "vue"
  import { useRouter,useRoute } from 'vue-router';
  const { proxy } = getCurrentInstance();

  // 分页参数
  const pageParams = ref({
    pageNo: 1,
    pageSize: 10
  })


  const api={
    loadPostList: "/post/loadAllPost",
  }

  // 数据状态
  const postList = ref([])
  const loading = ref(false)
  const hasMore = ref(true)
  const searchKey = ref('')

  // 初始化加载
  onMounted(() => {
    loadPosts(),
    getCategories()
  })

  // 加载更多
  const loadMore = () => {
    if (!loading.value && hasMore.value) {
      pageParams.value.pageNo++
      loadPosts()
    }
  }

  // 搜索处理
  const handleSearch = () => {
    postList.value = []
    pageParams.value.pageNo = 1
    loadPosts()
  }

  // 加载帖子数据
  const loadPosts = async () => {
    loading.value = true
    try {
      const res = await proxy.Request({
        url: api.loadPostList,
        params: {
          ...pageParams.value,
          titleFuzzy: searchKey.value,
          categoryId:selectedCategory.value==0?null:selectedCategory.value,
        }
      })

      postList.value = [...postList.value, ...res.data.list]
      hasMore.value = res.data.hasMore
    } finally {
      loading.value = false
    }
  }

  const router=useRouter();
  const route=useRoute();
  const viewDetail=(postId)=>{
    router.push(
      {
        name:"PostDetail",
        params:{
          id:postId,
        }
      }
    )
  }
// 新增分类相关状态
const categories = ref([]) // 从后端获取的分类数据
const selectedCategory = ref(0) // 当前选中分类

// 获取分类数据
const getCategories = async () => {
  const res = await proxy.Request({
    url: '/category/loadAllCategory4Select'
  })
  categories.value = res.data || []
}

// 分类选择处理
const selectCategory = (categoryId) => {
  selectedCategory.value = categoryId === selectedCategory.value ? null : categoryId
  handleSearch() // 触发重新加载
}

  </script>

  <style scoped lang="scss">
  .post-list-container {
    width: 90%;
    margin: 0 auto;
    max-width: 1200px;
    padding: 20px 0;
  }

  .search-box {
    width: 60%;
    margin: 20px auto;
  }

  .post-list {
    min-height: calc(100vh - 100px);
  }

  .post-item {
    background: #fff;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
    transition: transform 0.3s;

    &:hover {
      transform: translateY(-3px);
    }
  }

  .post-header {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .username {
      margin: 0 10px;
      font-weight: 500;
      color: #409EFF;
    }

    .post-time {
      color: #909399;
      font-size: 0.9em;
    }
  }

  .post-title {
    color: #303133;
    margin-bottom: 10px;
  }

  .post-content {
    color: #606266;
    line-height: 1.6;
    margin-bottom: 15px;
  }

  .post-footer {
    .el-tag {
      margin-right: 8px;
    }
  }

  .loading-tip {
    text-align: center;
    padding: 20px;
    color: #909399;
  }

  .el-backtop {
    background: #fff;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);

    &:hover {
      background: #f5f7fa;
    }
  }


.search-box {
  width: 60%;
  max-width: 584px; // 谷歌搜索框最大宽度
  margin: 20px auto;
  transition: all 0.3s;

  // 深度选择器修改Element组件样式
  :deep(.el-input) {
    .el-input__wrapper {
      border-radius: 24px; // 谷歌式圆角
      box-shadow: 0 1px 6px rgba(32, 33, 36, 0.08); // 谷歌阴影
      border: 1px solid #dfe1e5; // 边框颜色
      background: #fff;
      padding: 0 24px;
      transition: all 0.3s;

      // 聚焦状态
      &.is-focus {
        box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
        border-color: rgba(223,225,229,0);
      }

      // 输入框内部样式
      .el-input__inner {
        height: 44px; // 谷歌输入框高度
        font-size: 16px;
        color: #202124; // 谷歌文字颜色
        &::placeholder {
          color: #70757a; // 谷歌占位符颜色
        }
      }

      // 前缀图标
      .el-input__prefix {
        left: 16px;
        .el-icon {
          color: #70757a;
          font-size: 18px;
          transition: color 0.3s;
        }
      }

      // 清除按钮
      .el-input__suffix {
        right: 16px;
        .el-input__clear {
          color: #70757a;
          &:hover {
            color: #202124;
          }
        }
      }
    }

    // 悬停状态
    &:hover .el-input__wrapper:not(.is-focus) {
      box-shadow: 0 1px 6px rgba(32, 33, 36, 0.18);
      border-color: rgba(223,225,229,0);
    }
  }

}

.meta-section {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    min-width: 120px;

    .stats {
      color: #909399;
      font-size: 0.9em;
      margin-bottom: 8px;

      .stat-item {
        margin-left: 12px;

        .el-icon {
          vertical-align: -0.15em;
          margin-right: 2px;
        }
      }
    }

    .detail-btn {
      padding: 6px 12px;
      border-radius: 16px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
      }
    }
  }
  .category-scroll {
  width: 100%;
  margin-bottom: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.category-list {
  display: flex;
  gap: 12px; /* 分类项之间的间距 */
  padding: 4px 0;
}

.category-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #606266;
  background-color: #fff;
  border: 1px solid #ebeef5;
  border-radius: 20px; /* 圆角 */
  cursor: pointer;
  transition: all 0.3s ease; /* 过渡效果 */
  white-space: nowrap; /* 防止文字换行 */

  &:hover {
    background-color: #409eff; /* 悬停背景色 */
    color: #fff; /* 悬停文字颜色 */
    border-color: #409eff; /* 悬停边框颜色 */
  }

  &.active {
    background-color: #409eff; /* 选中背景色 */
    color: #fff; /* 选中文字颜色 */
    border-color: #409eff; /* 选中边框颜色 */
    font-weight: 500; /* 选中文字加粗 */
  }
}

/* 自定义滚动条样式 */
:deep(.el-scrollbar__bar) {
  background-color: rgba(144, 147, 153, 0.3); /* 滚动条颜色 */
}

:deep(.el-scrollbar__thumb) {
  background-color: rgba(144, 147, 153, 0.5); /* 滚动条滑块颜色 */
  &:hover {
    background-color: rgba(144, 147, 153, 0.8); /* 悬停时滑块颜色 */
  }
}

</style>
